package st.business.tepth.business.taskallocation.detail;

import java.util.List;

import st.business.tepth.base.OnDataConvertListener;
import st.shadow.library.ui.recycler.MultipleItemEntity;

/**
 * Author: shadow
 * Email:  1025431817@qq.com
 * Time:   2019-02-26 11:59
 */

public class AllocationDetailPresenter extends AllocationDetailContract.Presenter {
    private final AllocationDetailContract.View mView;
    private final AllocationDetailContract.Model mModel;

    public AllocationDetailPresenter(AllocationDetailContract.View view) {
        mView = view;
        mModel = new AllocationDetailModel();
    }

    @Override
    void allocationTasks(String token, int repairId, String repairTaskList) {
        addDisposable(mModel.allocationTasks(token, repairId,
                repairTaskList, new OnDataConvertListener<String>() {
                    @Override
                    public void convert(String s) {
                        if (mView != null) {
                            mView.showAllocationSucceed();
                        }
                    }

                    @Override
                    public void error(int code, String msg) {
                        if (mView != null) {
                            mView.showError(msg);
                        }
                    }
                }));
    }

    @Override
    void getTaskList(String token, int repairId) {
        addDisposable(mModel.getTaskList(token, repairId,
                new OnDataConvertListener<List<MultipleItemEntity>>() {
                    @Override
                    public void convert(List<MultipleItemEntity> list) {
                        if (mView != null) {
                            mView.showTaskList(list);
                        }
                    }

                    @Override
                    public void error(int code, String msg) {
                        if (mView != null) {
                            mView.showTaskList(null);
                            mView.showError(msg);
                        }
                    }
                }));
    }

    @Override
    void cancelAllocate(String token, int repairTaskId, int userId, final int position) {
        addDisposable(mModel.cancelAllocate(token, repairTaskId, userId,
                new OnDataConvertListener<String>() {
                    @Override
                    public void convert(String str) {
                        if (mView != null) {
                            mView.cancelAllocateSucceed(position);
                        }
                    }

                    @Override
                    public void error(int code, String msg) {
                        if (mView != null) {
                            mView.showError(msg);
                        }
                    }
                }));
    }
}
